package org.dtrd.modules.record.job;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.record.entity.bean.*;
import org.dtrd.modules.record.service.*;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author qr
 * @date 2022/4/18 16:30
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Component
@Slf4j
public class MockRecordJob implements Job {

    @Autowired
    private ISugarRecordService sugarRecordService;
    @Autowired
    private IFoodRecordService foodRecordService;
    @Autowired
    private ISportRecordService sportRecordService;
    @Autowired
    private IMedicalRecordService medicalRecordService;
    @Autowired
    private IInsulinRecordService insulinRecordService;
    @Autowired
    private IBloodPressureRecordService bloodPressureRecordService;
    @Autowired
    private IKetoneRecordService ketoneRecordService;
    @Autowired
    private IWaistlineRecordService waistlineRecordService;
    @Autowired
    private IHeightWeightRecordService heightWeightRecordService;
    @Autowired
    private ISleepRecordService sleepRecordService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap dataMap = context.getMergedJobDataMap();
        String patientIdStr = dataMap.getString("parameter");
        List<Integer> patientIds  = null;
        try {
            String[] strs = patientIdStr.split(",");
            patientIds = Arrays.stream(strs).map(Integer::parseInt).collect(Collectors.toList());
        }catch (Exception e) {
            log.error("保存日常数据定时任务参数格式错误: {},", patientIdStr);
            return;
        }
        for (Integer patientId : patientIds) {
            Date recordTime = new Date();
            //region 血糖
            sugarRecordService.saveSugarRecord(new SugarValueBean(recordTime, randomSugar(), Constant.SugarRecordType.BEFOREBREAKFAST, patientId));
            sugarRecordService.saveSugarRecord(new SugarValueBean(recordTime, randomSugar(), Constant.SugarRecordType.AFTERBREAKFAST, patientId));
            sugarRecordService.saveSugarRecord(new SugarValueBean(recordTime, randomSugar(), Constant.SugarRecordType.BEFORELUNCH, patientId));
            sugarRecordService.saveSugarRecord(new SugarValueBean(recordTime, randomSugar(), Constant.SugarRecordType.AFTERLUNCH, patientId));
            sugarRecordService.saveSugarRecord(new SugarValueBean(recordTime, randomSugar(), Constant.SugarRecordType.BEFOREDINNER, patientId));
            sugarRecordService.saveSugarRecord(new SugarValueBean(recordTime, randomSugar(), Constant.SugarRecordType.AFTERDINNER, patientId));
            sugarRecordService.saveSugarRecord(new SugarValueBean(recordTime, randomSugar(), Constant.SugarRecordType.BEFORESLEEP, patientId));
            sugarRecordService.saveSugarRecord(new SugarValueBean(recordTime, randomSugar(), Constant.SugarRecordType.EARLYMORNING, patientId));
            //endregion
            //region 食物
            foodRecordService.saveFoodRecord(new DietRecordBean(patientId, Constant.FoodRecordType.BREAKFAST, recordTime, "https://images-gz.oss-cn-guangzhou.aliyuncs.com/temp/17zao_1637747873947_1649986175030.jpg"));
            foodRecordService.saveFoodRecord(new DietRecordBean(patientId, Constant.FoodRecordType.LUNCH, recordTime, "https://images-gz.oss-cn-guangzhou.aliyuncs.com/temp/16zao_1637747261701_1649986191628.jpg"));
            foodRecordService.saveFoodRecord(new DietRecordBean(patientId, Constant.FoodRecordType.DINNER, recordTime, "https://images-gz.oss-cn-guangzhou.aliyuncs.com/temp/2_1637825798438_1649986208471.jpg"));
            //endregion
            //region 运动
            String sportPhoto = "https://images-gz.oss-cn-guangzhou.aliyuncs.com/temp/R-C(20)_1649986232264.jpg";
            sportRecordService.saveOrUpdateSportRecord(new SportBean(patientId, recordTime, RandomUtil.randomInt(1, 3), sportPhoto));
            //endregion
            //region 用药
            String medicalPhoto = "https://images-gz.oss-cn-guangzhou.aliyuncs.com/temp/202031614823968_1649986601087.jpg";
            String medicalDesc = "异维A酸软胶囊";
            medicalRecordService.saveOrUpdateMedicalRecord(new MedicalRecordInfo(patientId, recordTime, medicalPhoto, medicalDesc, Constant.MedicalRecordType.MORNING));
            medicalRecordService.saveOrUpdateMedicalRecord(new MedicalRecordInfo(patientId, recordTime, medicalPhoto, medicalDesc, Constant.MedicalRecordType.NOON));
            medicalRecordService.saveOrUpdateMedicalRecord(new MedicalRecordInfo(patientId, recordTime, medicalPhoto, medicalDesc, Constant.MedicalRecordType.EVENING));
            medicalRecordService.saveOrUpdateMedicalRecord(new MedicalRecordInfo(patientId, recordTime, medicalPhoto, medicalDesc, Constant.MedicalRecordType.SLEEP));
            //endregion
            //region 胰岛素
            String insulinDesc = "门冬6u";
            insulinRecordService.saveOrUpdateInsulinRecord(new InsulinRecordBean(patientId, recordTime, Constant.InsulinRecordType.MORNING, insulinDesc));
            insulinRecordService.saveOrUpdateInsulinRecord(new InsulinRecordBean(patientId, recordTime, Constant.InsulinRecordType.NOON, insulinDesc));
            insulinRecordService.saveOrUpdateInsulinRecord(new InsulinRecordBean(patientId, recordTime, Constant.InsulinRecordType.EVENING, insulinDesc));
            insulinRecordService.saveOrUpdateInsulinRecord(new InsulinRecordBean(patientId, recordTime, Constant.InsulinRecordType.SLEEP, insulinDesc));
            //endregion
            //region 血压
            Integer bloodHigh = RandomUtil.randomInt(110, 150);
            Integer bloodLow = RandomUtil.randomInt(70, 90);
            Integer pluse = RandomUtil.randomInt(80, 110);
            bloodPressureRecordService.saveBloodPressureRecord(new BloodBean(patientId, recordTime, bloodHigh, bloodLow, pluse, Constant.BloodPressureRecordType.MORNING));
            bloodPressureRecordService.saveBloodPressureRecord(new BloodBean(patientId, recordTime, bloodHigh, bloodLow, pluse, Constant.BloodPressureRecordType.EVENING));
            //endregion
            //region 尿酮
            ketoneRecordService.saveOrUpdateKetone(new DailyKetoneRecordBean(patientId, recordTime, RandomUtil.randomInt(0, 6)));
            //endregion
            //region 腰围
            waistlineRecordService.saveWaistLineRecord(new WaistLineBean(patientId, recordTime, RandomUtil.randomInt(60, 110)));
            //endregion
            //region 体重
            heightWeightRecordService.saveWeight(new HeightBean(patientId, recordTime, randomWeight(), randomHeight()));
            //endregion
            //region 睡眠
            sleepRecordService.saveOrUpdateSleepRecord(new SleepBean(patientId, recordTime, RandomUtil.randomInt(1, 5)));
            //endregion
        }
    }

    private float randomSugar() {
        return RandomUtil.randomBigDecimal(new BigDecimal("1.7"), new BigDecimal("17.7")).floatValue();
    }

    private float randomWeight() {
        return RandomUtil.randomBigDecimal(new BigDecimal("50.5"), new BigDecimal("120.2")).floatValue();
    }

    private float randomHeight() {
        return RandomUtil.randomBigDecimal(new BigDecimal("145.5"), new BigDecimal("210.1")).floatValue();
    }
}
